﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.盘库管理
{
    /// <summary>
    /// 盘库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class StocktakingOrderDetailController : BaseController
    {
        private readonly IStocktakeResultRecordService stocktakeResultRecordService;
        private readonly IStocktakingOrderService stocktakingOrderService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="stocktakingOrderService"></param>
        /// <param name="stocktakeResultRecordService"></param>
        public StocktakingOrderDetailController(IWebHostEnvironment environment, IStocktakingOrderService stocktakingOrderService, IStocktakeResultRecordService stocktakeResultRecordService) : base(environment)
        {
            this.stocktakeResultRecordService = stocktakeResultRecordService;
            this.stocktakingOrderService = stocktakingOrderService;
        }

        /// <summary>
        /// 添加盘点单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStocktakingOrderDetail(StocktakingOrder m)
        {
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            string message = "";
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool IsOK = stocktakingOrderService.AddStocktakingOrder(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 获取盘点单列表
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="aid"></param>
        /// <param name="billtype"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakingOrderDetailList(long? wid, long? aid,int? billtype, string? name, int? status, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = stocktakingOrderService.GetStocktakingOrderList(wid, aid,billtype, name, status, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 删除盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelStocktakingOrderDetail(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = stocktakingOrderService.DelStocktakingOrder(id, out string message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "收货中或已完成的收货单不允许删除！", "500"));
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakingOrderDetailById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var mobj = stocktakingOrderService.GetStocktakingOrderById(id);
            if (mobj != null)
            {
                return Ok(BaseResult.Result(mobj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 修改盘点单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdStocktakingOrderDetail(StocktakingOrder m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.OrderSn))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            bool IsOK = stocktakingOrderService.UpdStocktakingOrder(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 盘点报告
        /// </summary>
        /// <param name="ordersn"></param>
        /// <param name="materialkey"></param>
        /// <param name="status"></param>
        /// <param name="isHandle"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakeResultRecordList(string? ordersn, string? materialkey, int? status, byte? isHandle, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = stocktakeResultRecordService.GetStocktakeResultRecordList(ordersn, materialkey, status, isHandle, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 枚举盘库单据类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmInBillType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmStockBillType>();

            return Ok(list);
        }

        /// <summary>
        /// 装备盘点导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StocktakingDown()
        {
            var list = stocktakingOrderService.GetStocktakingDown();
            if (list != null)
                return StocktakingDown(list);
            else
                return Ok();
        }

        /// <summary>
        /// 盘点信息管理导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StocktakingMsgDown()
        {
            var list = stocktakingOrderService.GetStocktakingDown();
            if (list != null)
                return StocktakingMsgDown(list);
            else
                return Ok();
        }


        /// <summary>
        /// 库存结余导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StocktakeResultDown()
        {
            var list = stocktakeResultRecordService.GetStocktakeResultRecords();
            if (list != null)
                return StocktakeResultDown(list);
            else
                return Ok();
        }



    }
}
